package com.kelaite.procurement.repository;

import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.kelaite.procurement.entity.TPurchaseOrder;
import com.kelaite.procurement.entity.TPurchaseOrderPayment;

public interface PurchaseOrderPaymentRepository extends
		JpaRepository<TPurchaseOrderPayment, Long> {

	public List<TPurchaseOrderPayment> findByTPurchaseOrderOrderByDisplayOrderNoAsc(
			TPurchaseOrder tPurchaseOrder);

	@Modifying
	@Query(value = "update t_purchase_order_payment o set o.orderPaymentRemark =:orderPaymentRemark, o.planTime =:planTime, o.realTime =:realTime, o.planAmount =:planAmount, o.billedAmount =:billedAmount, o.increamentTax =:increamentTax, o.amountWithoutTax =:amountWithoutTax, o.remark =:remark, o.gmtUpdateTime =:gmtUpdateTime, o.updatedBy =:updatedBy  where o.id =:id")
	public int updatePurchaseOrderPayment(@Param("id") Long id,
			@Param("orderPaymentRemark") String orderPaymentRemark,
			@Param("planTime") Date planTime, @Param("realTime") Date realTime,
			@Param("planAmount") Long planAmount,
			@Param("billedAmount") Long billedAmount,
			@Param("increamentTax") Float increamentTax,
			@Param("amountWithoutTax") Float amountWithoutTax,
			@Param("remark") String remark,
			@Param("gmtUpdateTime") Date gmtUpdateTime,
			@Param("updatedBy") String updatedBy);

	@Query(value = "select count(*) from t_purchase_order_payment where purchase_order_id =?1", nativeQuery = true)
	public int countByPurchaseOrderId(Long puchaseOrderId);

	@Query(value = "select count(*) from t_purchase_order_payment where purchase_order_id =?1 and display_order_no =?2", nativeQuery = true)
	public int countByPurchaseOrderIdAndDisplayOrderNo(Long puchaseOrderId,
			String displayOrderNo);

	@Query(value = "select sum(plan_amount) from t_purchase_order_payment where purchase_order_id =?1", nativeQuery = true)
	public Long getTotalPlanAmountForAddCheck(Long purchaseOrderId);

	@Query(value = "select sum(plan_amount) from t_purchase_order_payment where purchase_order_id =?1 and id !=?2", nativeQuery = true)
	public Long getTotalPlanAmountForUpdateCheck(Long purchaseOrderId,
			Long purchaseOrderPaymentId);
	
	@Modifying
	@Query(value = "update t_purchase_order_payment o set o.acceptanceReportAttachmentStatus =:acceptanceReportAttachmentStatus, o.gmtUpdateTime =:gmtUpdateTime, o.updatedBy =:updatedBy where o.id =:id")
	public int updatePurchaseReportStatus(@Param("id") Long id,
			@Param("acceptanceReportAttachmentStatus") int acceptanceReportAttachmentStatus,
			@Param("gmtUpdateTime") Date gmtUpdateTime,
			@Param("updatedBy") String updatedBy);
	
	@Modifying
	@Query(value = "update t_purchase_order_payment o set o.invoiceAttachmentStatus1 =:invoiceAttachmentStatus1, o.gmtUpdateTime =:gmtUpdateTime, o.updatedBy =:updatedBy where o.id =:id")
	public int updatePurchaseInvoiceStatus1(@Param("id") Long id,
			@Param("invoiceAttachmentStatus1") int invoiceAttachmentStatus1,
			@Param("gmtUpdateTime") Date gmtUpdateTime,
			@Param("updatedBy") String updatedBy);
	
	@Modifying
	@Query(value = "update t_purchase_order_payment o set o.invoiceAttachmentStatus2 =:invoiceAttachmentStatus2, o.gmtUpdateTime =:gmtUpdateTime, o.updatedBy =:updatedBy where o.id =:id")
	public int updatePurchaseInvoiceStatus2(@Param("id") Long id,
			@Param("invoiceAttachmentStatus2") int invoiceAttachmentStatus2,
			@Param("gmtUpdateTime") Date gmtUpdateTime,
			@Param("updatedBy") String updatedBy);
}
